package test;

public class InheritableThreadLocalExample {
    public static InheritableThreadLocal<Integer> inheritableThreadLocal = new InheritableThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }

        @Override
        protected Integer childValue(Integer parentValue) {
            return parentValue * 10; // 子线程中的变量值是父线程中变量值的10倍
        }
    };
    public static void main(String[] args) throws InterruptedException {
        inheritableThreadLocal.set(1);
        System.out.println("main thread: " + inheritableThreadLocal.get()); // 输出1

        // 创建一个新线程，并在其中获取InheritableThreadLocal变量的值
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("new thread: " + inheritableThreadLocal.get()); // 输出10
            }
        });
        thread.start();
        thread.join();
    }
}
